package org.compiere.model;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.model.CalloutBCSACRInputFormAppend3_2.Nodes;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Trx;
import org.erps.MakeSwift;

public class CalloutBCSACRInputFormAppend3_2 extends CalloutEngine {
	
	private static CLogger s_log = CLogger.getCLogger(MBCSFRInputForm.class);
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	
	public String calcColumnValue1(Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value) {
		String result = "";	 
		
		
		int ID = mTab.getRecord_ID();
		MBCSACRInputFormAppend3_2 line = new MBCSACRInputFormAppend3_2(ctx, ID, null);

		String sql = "update BCS_ACRInputFormAppend3_2 set Value1 = " + value + " where BCS_ACRInputFormAppend3_2_ID = " + ID;
		DB.executeUpdate(sql);
		
		line = new MBCSACRInputFormAppend3_2(ctx, ID, null);

		List<MBCSACRInputFormAppend3_2> formlines = getMBCSACRInputFormAppend3_2Lines(ctx, line.getBCS_ACRInputForm_Company_ID());
		List<Nodes> nodeslines = getNodesLines(ctx, line.getBCS_ACRInputForm_Company_ID());
		
		int parent_id = 0;
		
		for(int i=0; i < nodeslines.size(); i++)
			if(nodeslines.get(i).node_id == line.getBCS_ACRPositionsDict_ID()){
				parent_id = nodeslines.get(i).parent_id;
				break;
			}
				
		List<Integer> path = new ArrayList<Integer>();
		
		getPath(path, formlines, nodeslines, parent_id);
		
		for(int i=0; i < path.size(); i++)
			updateParentsValue1(formlines, nodeslines,	path.get(i));
			
 		mTab.dataRefreshAll(true);
		mTab.getRowCount();
		mTab.getCurrentRow();
		
		
		return result;
	}
	
	private void getPath(List<Integer> path,  List<MBCSACRInputFormAppend3_2> formlines,
			List<Nodes> nodeslines, int parent_id) {
		
			for(Nodes line: nodeslines)
				if(line.getNode_id() == parent_id){
					path.add(parent_id);
					getPath(path, formlines, nodeslines, line.getParent_id());
					break;
				}
		
	}

	public String calcColumnValue2(Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value) {
		String result = "";	 
		
		int ID = mTab.getRecord_ID();
		String sql = "update BCS_ACRInputFormAppend3_2 set Value2 = " + value + " where BCS_ACRInputFormAppend3_2_ID = " + ID;
		DB.executeUpdate(sql);
		

		MBCSACRInputFormAppend3_2 line = new MBCSACRInputFormAppend3_2(ctx, ID, null);

		List<MBCSACRInputFormAppend3_2> formlines = getMBCSACRInputFormAppend3_2Lines(ctx, line.getBCS_ACRInputForm_Company_ID());
		List<Nodes> nodeslines = getNodesLines(ctx, line.getBCS_ACRInputForm_Company_ID());
		
		int parent_id = 0;
		
		for(int i=0; i < nodeslines.size(); i++)
			if(nodeslines.get(i).node_id == line.getBCS_ACRPositionsDict_ID()){
				parent_id = nodeslines.get(i).parent_id;
				break;
			}
				
		List<Integer> path = new ArrayList<Integer>();
		
		getPath(path, formlines, nodeslines, parent_id);
		
		for(int i=0; i < path.size(); i++)
			updateParentsValue2(formlines, nodeslines,	path.get(i));

		mTab.dataRefreshAll(true);
		mTab.getRowCount();
		mTab.getCurrentRow();
		
		
		return result;
	}

	
	protected void updateParentsValue1(List<MBCSACRInputFormAppend3_2> lines, List<Nodes> list, int node_id){
		
		MBCSACRInputFormAppend3_2 line = null;
		int parent_id = 0;
		int summ = 0;
		
		for(int i=0; i < lines.size(); i++)
			if(lines.get(i).getBCS_ACRPositionsDict_ID() == node_id)
				line = lines.get(i);
			
		for(int i=0; i < list.size(); i++){
			if(list.get(i).parent_id == node_id){
				parent_id = list.get(i).getNode_id(); 
				
				for(MBCSACRInputFormAppend3_2 iline : lines )
					if(iline.getBCS_ACRPositionsDict_ID() == parent_id)
						summ += iline.getValue1();
			}
		}
				
		line.setValue1(summ);
		line.save();
		
	}

	protected void updateParentsValue2(List<MBCSACRInputFormAppend3_2> lines, List<Nodes> list, int node_id){
		MBCSACRInputFormAppend3_2 line = null;
		int parent_id = 0;
		int summ = 0;
		
		for(int i=0; i < lines.size(); i++)
			if(lines.get(i).getBCS_ACRPositionsDict_ID() == node_id)
				line = lines.get(i);
			
		for(int i=0; i < list.size(); i++){
			if(list.get(i).parent_id == node_id){
				parent_id = list.get(i).getNode_id(); 
				
				for(MBCSACRInputFormAppend3_2 iline : lines )
					if(iline.getBCS_ACRPositionsDict_ID() == parent_id)
						summ += iline.getValue2();
			}
		}
				
		line.setValue2(summ);
		line.save();
		
	}

	
	
	protected List<Nodes> getNodesLines(Properties ctx, int BCS_ACRInputForm_Company_ID){
		List<Nodes> list = new ArrayList<Nodes>();
		
			String sql = 
					"	SELECT n.node_id, n.parent_id	\n" 
				+	"	FROM adempiere.BCS_ACRInputFormAppend3_2 l 	\n"
				+	"	inner join adempiere.BCS_ACRPositionsDict d on d.BCS_ACRPositionsDict_ID = l.BCS_ACRPositionsDict_ID	\n"	
				+	"	inner join adempiere.Ad_TreeNodeBCSAC n on n.node_id = d.BCS_ACRPositionsDict_ID	\n"	
				+	"	WHERE l.BCS_ACRInputForm_Company_ID = " + BCS_ACRInputForm_Company_ID;
		
			try {
			pstmt = DB.prepareStatement(sql, null);
			rs = pstmt.executeQuery();
			
			while(rs.next()) {
					Nodes l = new Nodes(rs.getInt(1), rs.getInt(2));
					list.add(l);
			}
		} catch (Exception e) {
			s_log.log(Level.SEVERE, "Callout:	BCS_ACRInputFormAppend3_2", e);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}			
		return list;
	}

	protected List<MBCSACRInputFormAppend3_2> getMBCSACRInputFormAppend3_2Lines(Properties ctx, int BCSACRInputFormCompany){
		List<MBCSACRInputFormAppend3_2> list = new ArrayList<MBCSACRInputFormAppend3_2>();
		
			String sql = "SELECT BCS_ACRInputFormAppend3_2_ID FROM BCS_ACRInputFormAppend3_2 WHERE BCS_ACRInputForm_Company_ID = " + BCSACRInputFormCompany;
		
			try {
			pstmt = DB.prepareStatement(sql, null);
			rs = pstmt.executeQuery();
			
			while(rs.next()) {
					MBCSACRInputFormAppend3_2 l = new MBCSACRInputFormAppend3_2(ctx, rs.getInt(1), null);
					list.add(l);
			}
		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "Callout:	BCS_ACRInputFormAppend3_2", e);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}			
		return list;
	}
	
	class Nodes{
		int node_id;
		int parent_id;
		
		public Nodes(int node_id, int parent_id){
			this.node_id = node_id;
			this.parent_id = parent_id;
			
		}
		
		public int getNode_id() {
			return node_id;
		}
		public void setNode_id(int node_id) {
			this.node_id = node_id;
		}
		public int getParent_id() {
			return parent_id;
		}
		public void setParent_id(int parent_id) {
			this.parent_id = parent_id;
		}
		
		
	}
	
	
}
